/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Chat;

import Serveur.I_chat;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Anne-Charlotte
 */
public class InterfacePrivee extends javax.swing.JFrame {
    
    private String login;
    private String destinataire;
    private I_chat sha;
    private ArrayList<String> historique;
    
    /**
     * Creates new form InterfacePrivee
     */
    public InterfacePrivee(I_chat chat, String dest, String login) {
        initComponents();
        this.login=login;
        this.sha = chat;
        this.destinataire = dest;
        historique = new ArrayList<String>();
        
        InterfacePrivee.this.setLocationRelativeTo(null);
        InterfacePrivee.this.setTitle("Discussion privée : "+ destinataire);
        this.jTextAreaAffichageMessagesPrives.setEditable(false);
        this.jTextAreaEcrireMessagePrive.requestFocus();
        
        javax.swing.Timer t = new javax.swing.Timer(1000, new InterfacePrivee.majChat());
        t.start();
        
    }
    
    /** Actualise la messagerie privée ssi le message reçu nous est destiné
     * @throws RemoteException 
     */
    private void actualiserChat() throws RemoteException{
        historique = sha.retourHistorique();
        jTextAreaAffichageMessagesPrives.setText("");
        for(int i=0;i<historique.size();i++ ){
                if(!historique.get(i).startsWith("message")){
                    historique.remove(i);
                }
        }
        for(int i=0;i<historique.size();i++ ){
                jTextAreaAffichageMessagesPrives.append(historique.get(i)+"\n");
        }
    }
    
    /** Event d'actualisation du chat
     */
    class majChat implements ActionListener {
        @Override
    	public void actionPerformed(ActionEvent e) {
            try {
                InterfacePrivee.this.actualiserChat();
            } 
            catch (RemoteException ex) {
                Logger.getLogger(InterfacePrincipale.class.getName()).log(Level.SEVERE, null, ex);
            }
    	}
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jScrollPaneAffichageMessagesPrives = new javax.swing.JScrollPane();
        jTextAreaAffichageMessagesPrives = new javax.swing.JTextArea();
        jButtonEnvoiPrive = new javax.swing.JButton();
        jScrollPaneEcrireMessagePrive = new javax.swing.JScrollPane();
        jTextAreaEcrireMessagePrive = new javax.swing.JTextArea();
        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new java.awt.GridBagLayout());

        jTextAreaAffichageMessagesPrives.setColumns(20);
        jTextAreaAffichageMessagesPrives.setRows(5);
        jScrollPaneAffichageMessagesPrives.setViewportView(jTextAreaAffichageMessagesPrives);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.gridheight = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 300;
        gridBagConstraints.ipady = 300;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        getContentPane().add(jScrollPaneAffichageMessagesPrives, gridBagConstraints);

        jButtonEnvoiPrive.setText("Envoyer");
        jButtonEnvoiPrive.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonEnvoiPriveActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        getContentPane().add(jButtonEnvoiPrive, gridBagConstraints);

        jTextAreaEcrireMessagePrive.setColumns(20);
        jTextAreaEcrireMessagePrive.setRows(5);
        jScrollPaneEcrireMessagePrive.setViewportView(jTextAreaEcrireMessagePrive);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.ipadx = 300;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        getContentPane().add(jScrollPaneEcrireMessagePrive, gridBagConstraints);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 13)); // NOI18N
        jLabel1.setText("Messagerie privée :");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        getContentPane().add(jLabel1, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        getContentPane().add(jSeparator1, gridBagConstraints);

        jButton1.setText("Femer la fenêtre");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
        gridBagConstraints.insets = new java.awt.Insets(0, 10, 10, 10);
        getContentPane().add(jButton1, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonEnvoiPriveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEnvoiPriveActionPerformed
        String message = this.jTextAreaEcrireMessagePrive.getText();
        
        //envoi au(x) client(s)
        if (!message.isEmpty()){
            try {
                sha.messagePrive(message, login, destinataire, 'l');
                jTextAreaEcrireMessagePrive.setText("");
            } catch (RemoteException ex) {
                Logger.getLogger(InterfacePrivee.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_jButtonEnvoiPriveActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        InterfacePrivee.this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButtonEnvoiPrive;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPaneAffichageMessagesPrives;
    private javax.swing.JScrollPane jScrollPaneEcrireMessagePrive;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextArea jTextAreaAffichageMessagesPrives;
    private javax.swing.JTextArea jTextAreaEcrireMessagePrive;
    // End of variables declaration//GEN-END:variables
}
